let userName = document.getElementById("userName")
let userPwd = document.getElementById("userPwd")
let login = document.getElementById("login")
let g = document.getElementsByClassName("g")[0];
let showimg = document.getElementsByClassName("showimg")[0];
let b_img = document.getElementById("b_img");
let index = 0;
g.onclick = function () {
    if (userName.value == "") {
        alert("用户名不能为空")
        return
    } else if (userPwd.value == "") {
        alert("用户密码不能为空")
        return
    } else {
        let xhr = new XMLHttpRequest()
        xhr.onreadystatechange = function () {
            if (xhr.readyState == 4 && xhr.status == 200) {
                if (xhr.responseText === "用户名或密码输入错误") {
                    alert(xhr.responseText)
                    return
                } else if (xhr.responseText) {
                    let data = JSON.parse(xhr.responseText)
                    showimg.src = data.userimg;
                    localStorage.setItem("userId", data.id)
                    setTimeout(() => {
                        location.href = "../index.html";
                        userName.value = "";
                        userPwd.value = "";
                    }, 1000)
                }
            }
        }
        xhr.open("POST", "/api/admin/login", true)
        xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded")
        xhr.send(`userName=${userName.value}&userPwd=${userPwd.value}`)
        return
    }
}

let imgArr = [
    "../img/tutu1.jpg",
    "../img/tutu2.jpg",
    "../img/tutu3.jpg",
    "../img/tutu4.jpg",
    "../img/tutu5.jpg",
    "../img/tutu6.jpg",
    "../img/tutu7.jpg",
    "../img/tutu8.jpg",
    "../img/tutu9.jpg",
    "../img/tutu10.jpg",
]
setInterval(() => {
    index++;
    b_img.src = imgArr[index];
    if (index == 9) {
        index = -1;
    }
}, 1000);